#define __main_c

#include "includeAll.h"

code const uint16_t functionTime[5][3]=
{
    {0,0,0},
    {180,120,360},//3,2,6
    {90,60,150},//1.5,1,2.5
    {960,840,1440},//16,14,24
    {210,120,240},//3.5,2,4
};

void main(void)
{
    CFGWL = (CFGWL & 0x3F) | 0x80;	//WDT enable in Fast/Slow mode,disable in Idle/Stop mode
    CLRWDT = 1;				//clear watchdog
    ChangeToFastClk();
    InitIoport ();

    InitRegister ();
    EA = 1;				//global interrupt enable
    Buzz(25);
   // F_displayAll();
    //Delay();
    // Delay();
    //Delay();
   // PowerOff();
   bSelfCheck=0;
   bSelfCheckSure=0;
	if((check1==1)&&(check2==2)&&(check3==3)&&(check4==4)&&(check5==5))
	{
		
		
		
	}
	else
	{
		startTime=0;
		Buzz(25);
		PowerOff();
	}
    while(1)
    {
			SelfCheck();
			if(function>0)
			{
				check1=1;
				check2=2;
				check3=3;
				check4=4;
				check5=5;
			}
			else
			{
				check1=0;
				check2=0;
				check3=0;
				check4=0;
				check5=0;
			}
		//DisplayNumber(curTemp);
		if(startTime==0)
		{
			  F_displayAll();
		}
		if(startTime==1)
		{
			PowerOff();
			startTime=2;
		}
		if((bAdOff==0)&&(bAdShort==0))
		{
			bAdGood=1;
			bAdBuzz=0;
		}
		else 
		{
			
			bAdGood=0;
			bAdBuzz=1;
		}
//		 F_setLedZhengDou() ;
// F_setLedFaJiao()		;
//F_setLedGeShuiDun()
        CLRWDT = 1;
        GetKey();
        timerControl();
        LiuCheng();
        FinishBuzz();
        //keyValue=1;
		if(bSelfCheck==0)
		{
			if(keyValue==D_OnOff)
			{
				bSelfCheck=1;
				bSelfCheckSure=1;
			}
			
		}
        switch(keyValue)
        {
        case D_Function:

            FunctionChoice();
            break;

        case D_Jian:
            Jian();
            break;

        case D_Jia:
            Jia();
            break;

        case D_OnOff:
            FunctionOnOff();
            break;
        }

        if(longKeyHoldTime>=6)
        {
            longKeyHoldTime=0;

            if(longKeyValue==D_Jian)
            {
                Jian();
            }
            if(longKeyValue==D_Jia)
            {
                Jia();
            }


        }
        keyValue=0;
        //DisplayNumber
    }

}
void timerControl()
{
    if (timer40ms >= D_40ms)
    {
        timer40ms = 0;
        if(bBuzz==0)	getTemp();
        if((bFunctionSure)&&(bAdGood)&&(step!=3))
        {
            if(bKeyTemp)
            {
                DisplayNumber(curTemp);
            }
            else
            {
                DisplayTime(workTime);
            }
        }
		if((function==0)&&(startTime==2)&&(bAdGood))
		{
			F_displayOF();
		}
		if((function>0)&&(bFunctionSure==0)&&(startTime==2)&&(bAdGood))
		{
			  DisplayTime(workTime);
		}
        if(longKeyValue==0)
        {
            longKeyJudgeTime++;
        }
        else
        {
            longKeyHoldTime++;
        }

    }
    if (timer500ms >= D_500ms)
    {
        timer500ms = 0;
		
        bSecFinishBuzz=1;
        bSecFlash500ms=~bSecFlash500ms;
        if((flashFunctionTime>0)&&(bAdGood))
        {
            LED();


        }
		if(bAdOff)
		{
			if(bSecFlash500ms)
			{
				F_displayAdOff()   ;
			}
			else
			{
				F_displayClrAll()   ;
			}
				
		}
		if(bAdShort)
		{
			if(bSecFlash500ms)
			{
				F_displayAdShort();
			}
			else
			{
				F_displayClrAll()   ;
			}
		}

    }
    if (timer1s >= D_1s)
    {
        timer1s = 0;
		bUpdateCurTemp=1;
		if(bSelfCheck==0)
		{
			bSelfCheck=1;
		}
		if(startTime==0)
		{
			startTime++;
		}
        if(liuChengTime>0)
        {
            liuChengTime--;
        }
        if((flashFunctionTime>0)&&(bAdGood))
        {
            flashFunctionTime--;
            if(flashFunctionTime==0)
            {
                bSecFlash500ms=1;
                LED();
                bPowerOnOff=1;
                bFunctionSure=1;
            }
        }
        timer1m++;
        if (timer1m >= D_1m)
        {
            timer1m = 0;
            if(timer30m>0)
            {
                timer30m--;
            }
            if((bWorkTimeDec)&&(workTime>0)&&(bAdGood))
            {
                workTime--;
            }
        }
    }
}



void PowerOff()
{
    F_displayOF();
    F_displayClrAllLed();
    function=0;
    bFunctionSure=0;
    flashFunctionTime=0;
    bPowerOnOff=0;
    workTime=0;
    minWorkTime=0;
    maxWorkTime=0;
    liuChengTime=0;
    bKeyTemp=0;
    timer30m=0;
    bFinishBuzz=0;
    finishBuzzNum=0;
    finishBuzzCycle=0;
	step=0;
	bWorkTimeDec=0;
	holdWorkTime=0;
	AdBuzzNum=0;
	bAdBuzz=0;
    InitTriac();
	
	
	
	 timer40ms=0;
 timer500ms=0;
 timer1s=0;
  timer1m=0;

 
}

void FunctionChoice()
{
    if(bFunctionSure==0)
    {


        Buzz(25);
        function++;
        //bPowerOnOff=1;
        flashFunctionTime=10;
        if(function>4)
        {
            function=1;
        }
        else if(function<0)
        {
            function=4;
        }
        bSecFlash500ms=1;
        timer500ms=0;
        LED();
        workTime=functionTime[function][0];
        minWorkTime=functionTime[function][1];
        maxWorkTime=functionTime[function][2];
        DisplayTime(workTime);
    }
}


void Jia()
{
    if((function>0)&&(bFunctionSure==0))
    {
        flashFunctionTime=10;
        Buzz(25);
        if(function!=D_FaJiao)
        {
            if(workTime<maxWorkTime)
            {
                workTime+=6;
            }
        }
        else
        {
            if(workTime<maxWorkTime)
            {
                workTime+=60;
            }
        }
        DisplayTime(workTime);


    }
}

void Jian()
{
    if((function>0)&&(bFunctionSure==0))
    {
        flashFunctionTime=10;
        Buzz(25);
        if(function!=D_FaJiao)
        {
            if(workTime>minWorkTime)
            {
                workTime-=6;
            }
        }
        else
        {
            if(workTime>minWorkTime)
            {
                workTime-=60;
            }
        }
        DisplayTime(workTime);

    }
}



void FunctionOnOff()
{

    if(function>0)
    {


        Buzz(25);
        bPowerOnOff=~bPowerOnOff;

        if(bPowerOnOff==1)
        {
            flashFunctionTime=0;
            bSecFlash500ms=1;
            LED();
            bFunctionSure=1;

        }
        else
        {
            PowerOff();
        }

    }
}

void LED()
{
    if(bSecFlash500ms)
    {


        switch(function)
        {
        case D_PaoDou:
            F_setLedPaoDou();
            break;
        case D_ZhengDou:
            F_setLedZhengDou();
            break;
        case D_FaJiao:
            F_setLedFaJiao();
            break;
        case D_GeShuiDun:
            F_setLedGeShuiDun();
            break;

        }
    }
    else
    {
        F_displayClrAllLed();
    }
}



void FinishBuzz()
{
    if((bFinishBuzz)&&(timer30m==0))
    {

        if(bSecFinishBuzz)
        {
            bSecFinishBuzz=0;
            finishBuzzNum++;

            if(finishBuzzNum<=4)
            {
                Buzz(25);
            }
            else if(finishBuzzNum>=7)
            {
                finishBuzzNum=0;
                finishBuzzCycle++;
                if(finishBuzzCycle>=40)
                {
                    timer30m=30;
                    finishBuzzNum=0;
                    finishBuzzCycle=0;
                }
            }


        }
    }
	
	// if(bAdBuzz)
	// {
		 // if(bSecFinishBuzz)
        // {
		 // bSecFinishBuzz=0;
           // AdBuzzNum++;

         // if(AdBuzzNum<=4)
            // {
                // Buzz(25);
            // }
			 // else if(AdBuzzNum>=7)
            // {
				// AdBuzzNum=0;
			// }
		// }
	// }
	// else
	// {
		// AdBuzzNum=0;
	// }
}


void LiuCheng()
{
    if((bFunctionSure)&&(bAdGood))
    {


        switch(function)
        {
        case D_PaoDou:
             FunctionPaoDou(42 ,43 ,41);
            break;
        case D_ZhengDou:
            FunctionZhengDou();
            break;
        case D_FaJiao:
            FunctionPaoDou(40 ,41 ,39);
            break;
        case D_GeShuiDun:
            FuinctionGeShuiDun();
            break;
        }
    }
}




void FunctionPaoDou(uint8_t temp1 ,uint8_t temp2 ,uint8_t temp3)
{
    if(step==0)
    {

        bWattSetted=1;
        if(curTemp<temp1)
        {
           Cycle(10,15);
        }

        else
        {
            step++;
            bWattSetted=0;
        }
    }
    else if(step==1)
    {
        bWorkTimeDec=1;
        if(curTemp<temp2)
        {
            bWattSetted=1;
            fireType=D_100W;
        }
        else if(curTemp>temp3)
        {
            bWattSetted=0;
            fireType=D_0W;
        }

        if(workTime<=0)
        {
            bFinishBuzz=1;
            step=3;


        }

    }
   
     FinishDisplay();
}



void FunctionZhengDou()
{
    if(step==0)
    {

        bWattSetted=1;
        if(curTemp<93)
        {
            fireType=D_300W;
        }

        else
        {
            step++;
            if(bWorkTimeDec==0)
            {
                holdWorkTime=workTime;
            }
            bWorkTimeDec=1;
            holdWorkTime=workTime;
            bWattSetted=0;
            Buzz(50);
        }
    }
    else if(step==1)
    {
        if(workTime>(holdWorkTime-30))
        {
            bWattSetted=1;
            fireType=D_300W;
        }
        else
        {
            bWattSetted=0;
            fireType=D_0W;
            step++;
        }
    }
    else if(step==2)
    {
        bWattSetted=1;
        Cycle(60,40);


        if(workTime<=0)
        {
            bFinishBuzz=1;

            step++;


        }
    }
       FinishDisplay();
}


// void FunctionFaJiao()
// {
    // if(step==0)
    // {

        // bWattSetted=1;
        // if(curTemp<temp1)
        // {
            // if(liuChengTime==0)
            // {
                // steps=~steps;
                // if(steps==1)
                // {
                    // fireType=D_300W;
                    // liuChengTime=10;
                // }
                // else
                // {
                    // fireType=D_0W;
                    // liuChengTime=15;
                // }
            // }

        // }

        // else
        // {
            // step++;
            // bWattSetted=0;
        // }
    // }
    // else if(step==1)
    // {
        // bWorkTimeDec=1;
        // if(curTemp<temp2)
        // {
            // bWattSetted=1;
            // fireType=D_100W;
        // }
        // else if(curTemp>temp3)
        // {
            // bWattSetted=0;
            // fireType=D_0W;
        // }

        // if(workTime<=0)
        // {
            // bFinishBuzz=1;
            // step++;


        // }

    // }
    // else if(step==2)
    // {
        // if(bSecFlash500ms)
        // {
            // F_displayOF();
            // F_displayClrAllLed();
        // }
        // else
        // {
            // F_displayClrAll();
        // }
    // }
// }


void FuinctionGeShuiDun()
{
    if(step==0)
    {
        bWattSetted=1;
        if(bWorkTimeDec==0)
        {
            holdWorkTime=workTime;
        }
        bWorkTimeDec=1;
        if(workTime>(holdWorkTime-30))
        {

            fireType=D_300W;
        }
        else
        {

            holdWorkTime=workTime;

            fireType=D_0W;
            bWattSetted=0;
            step++;
        }
    }
    else if(step==1)
    {
        bWattSetted=1;


        if(workTime>(holdWorkTime-30))
        {
          Cycle(25,18);
        }
        else
        {


            fireType=D_0W;
            bWattSetted=0;
            step++;

        }

    }
    else if(step==2)
    {
        bWattSetted=1;



      Cycle(25,30);

        if(workTime<=0)
        {


            fireType=D_0W;
            bWattSetted=0;
            bFinishBuzz=1;
            step++;


        }
    }
   
         FinishDisplay();
}


void  Cycle(uint8_t time1,uint8_t time2)
{
	      if(liuChengTime==0)
        {
            steps=~steps;
            if(steps==1)
            {
                fireType=D_300W;
                liuChengTime=time1;
            }
            else
            {
                fireType=D_0W;
                liuChengTime=time2;
            }
        }
}


void FinishDisplay()
{
	if(step==3)
	{
		
		//function=0;
	  if(bSecFlash500ms)
        {
            F_displayOF();
            F_displayClrAllLed();
        }
        else
        {
            F_displayClrAll();
        }
	}
}

void SelfCheck()
{
	if(bSelfCheckSure)
	{
		F_displayClrAllLed();
		DisplayNumber(00);
		Delay();
		DisplayNumber(11);
		Delay();
		DisplayNumber(22);
		Delay();
		DisplayNumber(33);
		Delay();
		DisplayNumber(44);
		Delay();
		DisplayNumber(55);
		Delay();
		DisplayNumber(66);
		Delay();
		DisplayNumber(77);
		Delay();
		DisplayNumber(88);
		Delay();
		DisplayNumber(99);
		Delay();
		F_displaybb()   ;
		 F_setLedPaoDou();
		 Delay();
		 F_setLedZhengDou();
		 Delay();
		 F_setLedFaJiao();
		 Delay();
		  F_setLedGeShuiDun();
		  Delay();
		   F_displayOF();
		    Delay();
			Buzz(50);
			Delay();
			Buzz(50);
			bSelfCheckSure=0;
	}
}